package com.pacs.service;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import com.pacs.tools.LogUtil;

/**
 * 线程池服务
 * @author yht
 *
 */
public class ThreadPoolService {
	private static ExecutorService POOLS = null;
	private static ScheduledExecutorService TIMER_POOLS = null;
	public ThreadPoolService(){
		POOLS		= Executors.newCachedThreadPool();
		TIMER_POOLS = Executors.newScheduledThreadPool(1);
	}

	/**
	 * 执行
	 * @param run
	 */
	public void execute(Runnable run){
		POOLS.execute(()->{
			try{
				run.run();
			}catch(Exception e){
				LogUtil.sysError(e.getMessage(), e);
			}
		});
	}
	
	/**
	 * 提交任务
	 * @param callable
	 * @return
	 */
	public <T> Future<T>  submit(Callable<T> callable){
		return POOLS.submit(callable);
	}
	
	/**
	 * 循环任务
	 * @param call
	 * @param delay
	 */
	public void schedule(Runnable call, long delay){
		TIMER_POOLS.scheduleAtFixedRate(()->{
			try{
				call.run();
			}catch(Exception e){
				LogUtil.sysError(e.getMessage(), e);
			}
		}, 0, delay, TimeUnit.MINUTES);
	}
}
